<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Q&A</title>
    <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
    <link rel="stylesheet" href="/css/style.css">
</head>
<body>
<section class="fixed" style="z-index:999;width: 100%;height: 100%;background-color: #000;position: fixed;left: 0;top: 0;"></section>
<div class="logo"></div>
<div class="wrap_box">
    <div class="search_box">
        <i class="search_icon"></i>
        <input type="text" value="搜索栏" onfocus="if(value=='搜索栏') {value=''}" onblur="if (value=='') {value='搜索栏'}">
    </div>
    <div class="question_list" id="example">
        {%foreach from=$article item=val%}
        <li>
            <div class="sub_hd">

                <p attrid="{%$val.id%}" onclick="changenum(this)"><span class="icon_q"></span>{%$val.qcontent%}</p>
                <div class="sub_bd clearfix dno">
                    <span class="icon_a"></span>
                    <p class="answer">{%$val.acontent%}</p>
                    <span class="view_num">{%$val.num%}</span>
                </div>
            </div>
        </li>
        {%/foreach%}
    </div>
</div>

</body>

<script src="/js/jquery-1.12.3.min.js"></script>
<script>
    $(window).load(function () {
        initEvent();
    });

    /**
     * 初始化
     */
    function initEvent(){
        selfAdaption();
        removeTopElement();
    }

    /**
     * 移除顶层元素
     */

    function removeTopElement() {
        $("section.fixed").remove();
    }

    /**
     *窗口发生改变
     */
    window.onresize = function () {
        selfAdaption();
    }

    /**
     * 自适应rem
     */
    function selfAdaption() {
        var screenWidth = document.documentElement.clientWidth;
        document.documentElement.style.fontSize = (100 / 640) * screenWidth + 'px';
    }

    /**
     * 展开项
     */
    $(".sub_hd p").on('click',function () {
        console.log(1);
        $(this).parent().children('.sub_bd').toggleClass('dno');
        $(this).parent().parent().siblings('li').find('.sub_bd').addClass('dno');
    })

    /**
     * 搜索
     */
    $("input[type=text]").change(function () {
        var searchText = $(this).val();//获取输入的搜索内容
        var $searchLi = "";//预备对象，用于存储匹配出的li

        if (searchText != "") {

            //获取所有匹配的li
            $searchLi = $("#example").find('li:contains('+ searchText +')');
            //将内容清空
            $("#example").html("");
        }

        //将获取的元素追加到列表中
        $("#example").html($searchLi).clone();


        //展开项
        $("#example .sub_hd p").on('click',function () {
            $(this).parent().children('.sub_bd').toggleClass('dno');
            $(this).parent().parent().siblings('li').find('.sub_bd').addClass('dno');
        })

        //判断搜索内容是否有效，若无效，输出not find
        if ($searchLi.length <= 0) {
            $("#example").html("<li>无搜索结果</li>");
        }
    })

    $(".search_icon").click(function () {
        $("searchText").change();
    })
    function changenum(obj) {
        $.ajax({
            type: "POST",
            url: "/doarticle.json",
            data: {id:parseInt(obj.getAttribute('attrid'))},
            dataType: 'text',
            success: function (e) {
               console.log(e)
            }
        });
    }
</script>
</html>